package com.kimehwa.studyboot.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaodongguan
 * @since 2021-07-10 20:12
 */
public class ThreadLocalUtil {
    private ThreadLocalUtil() {
    }

    public static final ThreadLocal<Map<String, Object>> THREAD_CONTEXT = new ThreadLocal<>();

    public static void put(String key, Object obj) {
        Map<String, Object> map = THREAD_CONTEXT.get();
        if (map == null) {
            map = new HashMap<>();
            /* 将这个map存进去 */
            THREAD_CONTEXT.set(map);
        }
        map.put(key, obj);
    }

    public static Object get(String key) {
        Map<String, Object> map = THREAD_CONTEXT.get();
        return map != null ? map.get(key) : null;

    }

    public static void remove(String key) {
        Map<String, Object> map = THREAD_CONTEXT.get();
        map.remove(key);
    }

    public static void clear() {
        THREAD_CONTEXT.remove();
    }

}
